package com.leyou.item.controller;

import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("spec")
public class SpecGroupController {

    @Autowired
    private SpecGroupService specGroupService;
    @Autowired
    private SpecParamService specParamService;

    /*
    根据分类Id查询该分类的参数组
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryByCid(@PathVariable("cid") Long cid){

        List<SpecGroup> list=this.specGroupService.queryGroupByCid(cid);
        if(CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }
    /*
    根据gid查询参数列表
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParamByGid(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "generic",required = false)Boolean generic,
            @RequestParam(value = "searching",required = false)Boolean searching){

        List<SpecParam>  params=specParamService.queryParam(gid,cid,generic,searching);
        if(CollectionUtils.isEmpty(params)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(params);
    }
    /*
        删除参数组
     */
    @DeleteMapping("group/{gid}")
    public ResponseEntity<Void> deleteGroupById(@PathVariable("gid")Long gid){

        this.specGroupService.deleteGroupById(gid);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /*
    新增参数组
     */
    @PostMapping("group")
    public ResponseEntity<Void> saveGroup(@RequestBody SpecGroup group){
        this.specGroupService.saveGroup(group);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /*
    修改参数组
     */
    @PutMapping("group")
    public ResponseEntity<Void> update(@RequestBody SpecGroup group){
        this.specGroupService.update(group);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
